package com.leo.blog.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.BlogDao;
import com.leo.blog.dao.BlogTypeDao;
import com.leo.blog.dto.BlogTypeExecution;
import com.leo.blog.entity.BlogType;
import com.leo.blog.enums.BlogTypeStateEnum;
import com.leo.blog.exceptions.BlogTypeOperationException;
import com.leo.blog.service.BlogTypeService;
import com.leo.blog.service.CacheService;
import com.leo.blog.util.ConstantUtil;

@Service//声明业务逻辑层
public class BlogTypeServiceImpl implements BlogTypeService{
	//注入dao层
	@Autowired
	private BlogTypeDao blogTypeDao;
	@Autowired
	private BlogDao blogDao;

	@Autowired
	private BlogTypeService blogTypeService;

	
	private static Logger logger = LoggerFactory.getLogger(BlogTypeServiceImpl.class);
	
	@Override
	@Transactional
	public List<BlogType> getBlogTypeList() {
		//定义redis的key
		String key = BLOGTYPELISTKEY;
		//定义接收对象
		List<BlogType> blogTypeList = null;

			//若不存在,则从数据库里面取出相应数据
		blogTypeList = blogTypeDao.queryBlogTypeList();


		return blogTypeList;
	}

	@Override
	public BlogTypeExecution getBlogTypeById(int blogTypeId) {
		BlogType blogType = blogTypeDao.queryBlogTypeById(blogTypeId);
		if(blogType != null) {
			return new BlogTypeExecution(BlogTypeStateEnum.SUCCESS, blogType);
		}else {
			return new BlogTypeExecution(BlogTypeStateEnum.NULL_BLOGTYPE);
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOGTYPE_UPDATE, userType = ConstantUtil.USERTYPE_ADMIN)
	public int editBlogType(String name, BlogType blogType) {
		//清除redis的key

		return blogTypeDao.updateBlogType(blogType);
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOGTYPE_DELETE, userType = ConstantUtil.USERTYPE_ADMIN)
	public int deleteBlogType(String name, int blogTypeId) {
		//清除redis的key
		return blogTypeDao.deleteBlogType(blogTypeId);
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOGTYPE_ADD, userType = ConstantUtil.USERTYPE_ADMIN)
	public int addBlogType(String name, BlogType blogType) {
		//赋初始值
		blogType.setBlogTypeNum(0);
		blogType.setCreateTime(new Date());
		//清除redis的key
		return blogTypeDao.insertBlogType(blogType);
	}

	@Override
	public int getBlogByBlogTypeCount(int blogTypeId) {
		return blogDao.queryBlogByBlogTypeCount(blogTypeId);
	}

}
